package com.qianyu.project.web.controller.mfy;

import com.qianyu.project.core.context.user.FlowUserProvider;
import com.qianyu.project.entity.DeviceMaintain;
import com.qianyu.project.entity.DeviceOilUsed;
import com.qianyu.project.service.*;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Map;

/**
 * @Author mfy
 * @desc
 */
@RestController
@RequestMapping("/deviceMaintains")
public class DeviceMaintainController {

    @Autowired
    private IDeviceMaintainService deviceMaintainService;
    @Autowired
    private FlowUserProvider flowUserProvider;
    @Autowired
    private IDeviceMaintainListService deviceMaintainListService;
    @Autowired
    private IAttachemenInfoService attachemenInfoService;


    @PostMapping("")
    public Result save(@RequestBody Map<String,Object> params){
        DeviceMaintain dm = new DeviceMaintain();
        Map<String,Object> mapDrp = (Map<String, Object>) params.get("Devicemaintain");
        dm.setPId(Integer.valueOf(mapDrp.get("pId")+""));
        dm.setMaintainName(mapDrp.get("maintainName")+"");
        dm.setFillUser(flowUserProvider.getFlowUser().getId().intValue());
        dm.setUpdateDate(LocalDate.now().toString());
        dm.setTotalPrice(BigDecimal.valueOf(Double.valueOf(mapDrp.get("price")+"")));
        dm.setTotalUpper(mapDrp.get("totalUpper")+"");
        dm.setComment(mapDrp.get("comment")+"");
        dm.setApproveStatus(1+"");
        deviceMaintainService.Mysave(dm,params);
        return ResultTool.success();
    }

    @PutMapping("")
    public Result update(@RequestBody Map<String,Object> params){
        DeviceMaintain dm = new DeviceMaintain();
        Map<String,Object> mapDrp = (Map<String, Object>) params.get("Devicemaintain");
        dm.setId(Integer.valueOf(mapDrp.get("id")+""));
        dm.setPId(Integer.valueOf(mapDrp.get("pId")+""));
        dm.setMaintainName(mapDrp.get("maintainName")+"");
        dm.setFillUser(flowUserProvider.getFlowUser().getId().intValue());
        dm.setUpdateDate(LocalDate.now().toString());
        dm.setTotalPrice(BigDecimal.valueOf(Double.valueOf(mapDrp.get("price")+"")));
        dm.setTotalUpper(mapDrp.get("totalUpper")+"");
        dm.setComment(mapDrp.get("comment")+"");
        dm.setApproveStatus(1+"");
        deviceMaintainService.saveOrUpdate(dm);
        deviceMaintainService.Myupdate(dm,params);
        return ResultTool.success();
    }
    @GetMapping("")
    public Result list(@RequestParam Map<String,String> params){
        return ResultTool.success(deviceMaintainService.Mylist(params));
    }
    @GetMapping("/listNopage")
    public Result listNopage(@RequestParam Map<String,String> params){
        return ResultTool.success(deviceMaintainService.listNopage(params));
    }

    @GetMapping("/selectEnclosures/{attachIds}")
    public Result selectEnclosures(@PathVariable String attachIds){
        return ResultTool.success(attachemenInfoService.selectEnclosures3(attachIds));
    }

    @GetMapping("/selectMaterialInfos/{id}")
    public Result selectMaterialInfos(@PathVariable String id){
        return ResultTool.success(deviceMaintainListService.selectMaterialInfos(id));
    }
}
